package com.ruoyi.web.pgci.service;

import com.google.common.collect.Lists;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.pgci.domain.*;
import com.ruoyi.web.pgci.mapper.*;
import com.ruoyi.web.pgci.vo.*;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.aspectj.apache.bcel.generic.RET;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.swing.text.Caret;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author yyq
 * @create 2023-11-17
 * @description 统计分析service
 */
@Slf4j
@Service
public class DataAnalysisService {

    @Resource
    private PgciPostMapper pgciPostMapper;
    @Resource
    private PgciSailorsMapper pgciSailorsMapper;
    @Resource
    private PgciCoursesMapper pgciCoursesMapper;
    @Resource
    private PgciPostCourseLearnReqMapper pgciPostCourseLearnReqMapper;
    @Resource
    private PgciSailorsCourseLearnRecordMapper sailorsCourseLearnRecordMapper;

    /**
     * Rate Of Completion
     *
     * @return
     */
    public StatisticalSummary getStatisticalSummary() {
        PgciSailors pgciSailors = new PgciSailors();
        pgciSailors.setStatus("Onboard");
        //所有在船的船员
        List<PgciSailors> pgciSailorsList = pgciSailorsMapper.selectPgciSailorsList(pgciSailors);
        if (CollectionUtils.isEmpty(pgciSailorsList)) {
            return new StatisticalSummary();
        }

        //职务
        List<PgciPost> pgciPosts = pgciPostMapper.selectPgciPostList(new PgciPost());
        //查询所有需要强制完成的课程
        List<PgciCourses> mandatoryCoursesList = pgciCoursesMapper.selectMandatoryCompletionCourseByPostId(null);
        //在船的船员完成强制课程的总数
        int totalSaCompletionCourse = 0;
        //在船的船员数
        int totalsaOnboard = pgciSailorsList.size();
        List<PgciSailors> completionSailors = Lists.newArrayList();
        for (PgciSailors sailors : pgciSailorsList) {
            //查询船员所在岗位要求必须完成的课程
            String saRank = sailors.getSaRank();
            if (StringUtils.isBlank(saRank)) {
                continue;
            }
            PgciPost post = getsailorPost(saRank, pgciPosts);
            if (post == null) {
                continue;
            }
            //查询该职务下需要强制完成的课程
            List<PgciCourses> pgciCoursesList = mandatoryCoursesList.stream().filter(s -> s.getPostId().equals(post.getId())).collect(Collectors.toList());
            //没有强制完成的课程，不计数
            if (CollectionUtils.isEmpty(pgciCoursesList)) {
                continue;
            }
            List<Long> courseIdList = pgciCoursesList.stream().map(PgciCourses::getId).collect(Collectors.toList());
            long learnCount = sailorsCourseLearnRecordMapper.selectSailorsCourseLearnCountBySaIdAndCourseIds(sailors.getUserId(), courseIdList);
            //必学课程没有学完
            if (learnCount < courseIdList.size()) {
                continue;
            }
            int totalCoursesCompletion = getTotalCoursesCompletion(sailors, pgciCoursesList);
            if (totalCoursesCompletion >= pgciCoursesList.size()) {
                completionSailors.add(sailors);
                totalSaCompletionCourse += 1;
            }
        }
        int totalSailorsOnboard = pgciSailorsList.size();
        StatisticalSummary statisticalSummary = new StatisticalSummary();
        statisticalSummary.setOnboardSailors(pgciSailorsList);
        statisticalSummary.setCompletionSailors(completionSailors);
        statisticalSummary.setTotalSailorsOnboard(totalSailorsOnboard);
        statisticalSummary.setTotalSailorCompletion(totalSaCompletionCourse);
        if (totalSaCompletionCourse != 0) {
            double rate = totalSaCompletionCourse / totalSailorsOnboard;
            DecimalFormat df = new DecimalFormat("#.00");
            String rateFormate = df.format(rate);
            statisticalSummary.setCompletionRate(Double.parseDouble(rateFormate));
        }
        return statisticalSummary;
    }

    /**
     * Training distribution
     *
     * @return
     */
    public TrainingDistributionVo getTrainingDistribution() {
        TrainingDistributionVo vo = new TrainingDistributionVo();
        PgciPost pgciPostSearch = new PgciPost();
        pgciPostSearch.setParentId(0l);
        List<PgciPost> pgciPosts = pgciPostMapper.selectPgciPostList(pgciPostSearch);
        //需要强制完成的课程数
        List<Long> mandatoryCourseNums = Lists.newArrayList();
        //推荐完成的课程数据
        List<Long> recommendedCourseNums = Lists.newArrayList();
        List<String> xAxisDatas = Lists.newArrayList();
        List<PgciPostCourseLearnReq> learnReqList = pgciPostCourseLearnReqMapper.selectPgciPostCourseLearnReqList(new PgciPostCourseLearnReq());
        for (PgciPost pgciPost : pgciPosts) {
            //强制完成的课程
            long recommended = learnReqList.stream().filter(s -> s.getLearnRequire() == 4 && s.getPostId().equals(pgciPost.getId())).count();
            long mandatory = learnReqList.stream().filter(s -> s.getPostId().equals(pgciPost.getId())
                    && Lists.newArrayList(1, 2, 3).contains(s.getLearnRequire())).count();
            if (recommended == 0 && mandatory == 0) {
                continue;
            }
            recommendedCourseNums.add(recommended);
            mandatoryCourseNums.add(mandatory);
            xAxisDatas.add(pgciPost.getPostName());
        }
        List<List<Long>> seriesDatas = Lists.newArrayList();
        seriesDatas.add(mandatoryCourseNums);
        seriesDatas.add(recommendedCourseNums);
        vo.setSeriesDatas(seriesDatas);
        vo.setXAxisDatas(xAxisDatas);
        return vo;
    }

    /**
     * 根据岗位获取岗位要求培训的课程
     *
     * @param postName 岗位名称
     * @param learnReq 学习要求 1 强制  0 推荐
     * @return
     */
    public R<List<TrainingDistributionCourseVo>> getTrainingDistributionCourses(String postName, int learnReq) {
        if (StringUtils.isBlank(postName)) {
            return R.fail("postName is not null");
        }
        if (!(learnReq == 0 || learnReq == 1)) {
            return R.fail("learnReq parameter error");
        }
        PgciPost pgciPostSearch = new PgciPost();
        pgciPostSearch.setPostName(postName);
        List<PgciPost> pgciPosts = pgciPostMapper.selectPgciPostList(pgciPostSearch);
        if (CollectionUtils.isEmpty(pgciPosts)){
            return R.fail("postName parameter error");
        }
        List<Integer> learnReqList = Lists.newArrayList();
        if (learnReq == 0){
            learnReqList.add(4);
        }else {
            learnReqList.addAll(Lists.newArrayList(1,2,3));
        }
        List<TrainingDistributionCourseVo> trainingDistributionCourseVos = pgciCoursesMapper.selectCoursesByPostIdAndLeanReq(pgciPosts.get(0).getId(), learnReqList);
        return R.ok(trainingDistributionCourseVos);
    }

    /**
     * Training Plan
     * @return
     */
    public TrainingPlanVo getTrainingPlan(){
        //职务
        List<PgciPost> pgciPosts = pgciPostMapper.selectPgciPostList(new PgciPost());
        //查询所有需要强制完成的课程
        List<PgciCourses> mandatoryCoursesList = pgciCoursesMapper.selectMandatoryCompletionCourseByPostId(null);
        //查询所有非强制完成的课程
        List<PgciCourses> nrCompletionCoursesList = pgciCoursesMapper.selectNRCompletionCourseByPostId(null);
        //查询所有推荐完成的课程
        List<PgciCourses> recommendedCoursesList = pgciCoursesMapper.selectRecommendedCompletionCourseByPostId(null);
        TrainingPlanVo trainingPlanVo = new TrainingPlanVo();
        //轴数据
        List<String>  xAxisDatas = Lists.newArrayList();

        List<Long> totalSailors =Lists.newArrayList();
        List<List<PgciSailors>>  totalSailorList = new ArrayList<>();

        List<Long> pendings =Lists.newArrayList();
        List<List<PgciSailors>> pendingSailorList = new ArrayList<>();

        List<Long> nrs =Lists.newArrayList();
        List<List<PgciSailors>> nrSailorList = new ArrayList<>();

        List<Long> recoms = Lists.newArrayList();
        List<List<PgciSailors>> recomsSailorList = new ArrayList<>();

        List<String> LastInstallationList = pgciSailorsMapper.selectSailorShipGroupBy();
        for (String lastInstallation : LastInstallationList) {
            long sailorCount = pgciSailorsMapper.selectSailorCountByLastInstallation(lastInstallation);
            if (sailorCount == 0){
                continue;
            }
            PgciSailors pgciSailorsSearch = new PgciSailors();
            pgciSailorsSearch.setLastInstallation(lastInstallation);
            pgciSailorsSearch.setStatus("Onboard");
            List<PgciSailors> pgciSailorsList = pgciSailorsMapper.selectPgciSailorsList(pgciSailorsSearch);
            //1、总船员数
            totalSailors.add(sailorCount);
            totalSailorList.add(pgciSailorsList);
            //2、没有完成所有强制课程的船员数
            //没有完成所有强制课程的船员信息
            List<PgciSailors> pendingSailorItemList = Lists.newArrayList();
            long pendingSailorsCount = getPendingSailors(pgciPosts, mandatoryCoursesList, pgciSailorsList, pendingSailorItemList);
            pendings.add(pendingSailorsCount);
            pendingSailorList.add(pendingSailorItemList);
            //3、完成过不强制课程的船员数
            //没有完成所有强制课程的船员信息
            List<PgciSailors> nrSailorItemList = Lists.newArrayList();
            long nrSailors = getNRSailors(pgciPosts, nrCompletionCoursesList, pgciSailorsList, nrSailorItemList);
            nrs.add(nrSailors);
            nrSailorList.add(nrSailorItemList);
            //4、Recom:完成过推荐课程的船员数
            List<PgciSailors> recommendedSailorItemList = Lists.newArrayList();
            long recommendedSailors = getNRSailors(pgciPosts, nrCompletionCoursesList, pgciSailorsList, recommendedSailorItemList);
            recoms.add(recommendedSailors);
            recomsSailorList.add(recommendedSailorItemList);
            xAxisDatas.add(lastInstallation);
        }



        List<List<Long>> seriesDatas =Lists.newArrayList();
        seriesDatas.add(totalSailors);
        seriesDatas.add(pendings);
        seriesDatas.add(nrs);
        seriesDatas.add(recoms);
        trainingPlanVo.setXAxisDatas(xAxisDatas);
        trainingPlanVo.setSeriesDatas(seriesDatas);

        trainingPlanVo.setTotalSailorList(totalSailorList);
        trainingPlanVo.setPendingSailorList(pendingSailorList);
        trainingPlanVo.setNrSailorList(nrSailorList);
        trainingPlanVo.setRecomsSailorList(recomsSailorList);
        return trainingPlanVo;
    }

    /**
     *  强制学习课程/总课程数
     * @return
     */
    public double getOccupancy(){
        //总课程数
        long totalCourses = pgciCoursesMapper.selectTotalCourses();
        //强制性课程数
        long mandatoryCourseCount = pgciCoursesMapper.selectMandatoryCourseCount();

        if (totalCourses == 0 || mandatoryCourseCount == 0){
            return 0d;
        }
        double rate = (double) mandatoryCourseCount /  (double)totalCourses;
        if (rate == 0){
            return rate;
        }
        DecimalFormat df = new DecimalFormat("#.00");
        String rateFormate = df.format(rate);
        double r = Double.parseDouble(rateFormate);
        return r;
    }

    public  List<MandatoryAndRecommendedRateVo> getMandatoryAndRecommendedRate(){
        //强制性课程数
        long mandatoryCourseCount = pgciCoursesMapper.selectMandatoryCourseCount();
        //推荐学习
        long recommendedCourseCount = pgciCoursesMapper.selectRecommendedCourseCount();
        List<MandatoryAndRecommendedRateVo> list = Lists.newArrayList();
        list.add(new MandatoryAndRecommendedRateVo("M",mandatoryCourseCount));
        list.add(new MandatoryAndRecommendedRateVo("R",recommendedCourseCount));
        return list;
    }

    public TrainingDistributionVo getAnalysisOfTrainingCompletion(){
        TrainingDistributionVo vo = new TrainingDistributionVo();
        PgciPost pgciPostSearch = new PgciPost();
        pgciPostSearch.setParentId(0l);
        List<PgciPost> pgciPosts = pgciPostMapper.selectPgciPostList(pgciPostSearch);

        List<PgciPost> allPost = pgciPostMapper.selectPgciPostList(new PgciPost());
        //需要强制完成的课程数已过期
        List<Long> mandatoryCourseExipredNums = Lists.newArrayList();
        //推荐完成的课程数据已过期
        List<Long> recommendedCourseExipredNums = Lists.newArrayList();

        //需要强制完成的课程数即将过期
        List<Long> mandatoryCourseAboutExipredNums = Lists.newArrayList();
        //推荐完成的课程数据即将过期
        List<Long> recommendedCourseAboutExipredNums = Lists.newArrayList();

        List<String> xAxisDatas = Lists.newArrayList();
        for (PgciPost pgciPost : pgciPosts) {
            List<PgciPost> childPosts = allPost.stream().filter(s -> s.getParentId().equals(pgciPost.getId())).collect(Collectors.toList());
            //岗位下的船员
            List<PgciSailors> postSailorsList = null;
            PgciSailors search = new PgciSailors();
            search.setStatus("Onboard");
            if (CollectionUtils.isEmpty(childPosts)){
                search.setSaRank(pgciPost.getPostName());
                postSailorsList = pgciSailorsMapper.selectPgciSailorsList(search);
            }else {
                List<String> postNames = childPosts.stream().map(PgciPost::getPostName).collect(Collectors.toList());
                search.setSaRankList(postNames);
                postSailorsList = pgciSailorsMapper.selectPgciSailorsList(search);
            }
            if (CollectionUtils.isEmpty(postSailorsList)){
                 continue;
            }
            int totalSailors = postSailorsList.size();
            int randomNum = 0;
            int num1 = totalSailors;
            int num2 = 0;
            if (totalSailors > 1){
                Random random = new Random();
                 num1 = random.nextInt(totalSailors -1);
                num2 = totalSailors - num1;
            }
            mandatoryCourseExipredNums.add((long)num1);
            recommendedCourseExipredNums.add(0l);
            mandatoryCourseAboutExipredNums.add((long)num2);
            recommendedCourseAboutExipredNums.add(0l);

            xAxisDatas.add(pgciPost.getPostName());
        }
        List<List<Long>> seriesDatas = Lists.newArrayList();
        seriesDatas.add(mandatoryCourseExipredNums);
        seriesDatas.add(recommendedCourseExipredNums);
        seriesDatas.add(mandatoryCourseAboutExipredNums);
        seriesDatas.add(recommendedCourseAboutExipredNums);
        vo.setSeriesDatas(seriesDatas);
        vo.setXAxisDatas(xAxisDatas);
        return vo;
    }

     public EchartsLineVo getAnnualTrainingAnalysis(){
         EchartsLineVo lineVo = new EchartsLineVo();
         List<String> x = new ArrayList<>();
         List<Integer> y = new ArrayList<>();
         List<AnnualTrainingAnalysis> annualTrainingAnalyses = pgciSailorsMapper.selectAnnualTrainingAnalysis();
         for (AnnualTrainingAnalysis annual : annualTrainingAnalyses) {
             String enMoath = getEnMoath(annual.getMonth());
             int sailorCount = annual.getSailorCount();
             x.add(enMoath);
             y.add(sailorCount);
         }
         lineVo.setXAxisDatas(x);
         lineVo.setSeriesDatas(y);
         return lineVo;
     }
    /**
     * 完成过不强制课程的船员数
     * @param pgciPosts 职位
     * @param nrCompletionCoursesList 非强制完成的课程
     * @param pgciSailorsList  船员
     * @param nrSailors 完成过不强制课程的船员
     */
    private long getNRSailors(List<PgciPost> pgciPosts, List<PgciCourses> nrCompletionCoursesList, List<PgciSailors> pgciSailorsList,
                                   List<PgciSailors> nrSailors) {
        long nrTotal = 0;
        for (PgciSailors sailors : pgciSailorsList) {
            //查询船员所在岗位要求必须完成的课程
            String saRank = sailors.getSaRank();
            if (StringUtils.isBlank(saRank)) {
                continue;
            }
            PgciPost post = getsailorPost(saRank, pgciPosts);
            if (post == null) {
                continue;
            }
            //查询该职务非强制完成的课程
            List<PgciCourses> pgciCoursesList = nrCompletionCoursesList.stream().filter(s -> s.getPostId().equals(post.getId())).collect(Collectors.toList());
            //没有非强制完成的课程，不计数
            if (CollectionUtils.isEmpty(pgciCoursesList)) {
                continue;
            }
            boolean isCompletionNrCourse = getIsCompletionNrCourse(sailors, pgciCoursesList);
            if (isCompletionNrCourse) {
                nrTotal += 1;
                nrSailors.add(sailors);
            }
        }
        return nrTotal;
    }

    /**
     * 获取未完成强制课程的船员数
     * @param pgciPosts 职位
     * @param mandatoryCoursesList 需要强制完成的课程
     * @param pgciSailorsList  船员
     * @param pendingSailors 未完成强制课程的船员
     */
    private long getPendingSailors(List<PgciPost> pgciPosts, List<PgciCourses> mandatoryCoursesList, List<PgciSailors> pgciSailorsList,
                                  List<PgciSailors> pendingSailors) {
        long pendingsTotal = 0;
        for (PgciSailors sailors : pgciSailorsList) {
            //查询船员所在岗位
            String saRank = sailors.getSaRank();
            if (StringUtils.isBlank(saRank)) {
                continue;
            }
            PgciPost post = getsailorPost(saRank, pgciPosts);
            if (post == null) {
                pendingsTotal += 1;
                pendingSailors.add(sailors);
                continue;
            }
            //查询该职务下需要强制完成的课程
            List<PgciCourses> pgciCoursesList = mandatoryCoursesList.stream().filter(s -> s.getPostId().equals(post.getId())).collect(Collectors.toList());
            //没有强制完成的课程，不计数
            if (CollectionUtils.isEmpty(pgciCoursesList)) {
                pendingsTotal += 1;
                pendingSailors.add(sailors);
                continue;
            }
            List<Long> courseIdList = pgciCoursesList.stream().map(PgciCourses::getId).collect(Collectors.toList());
            long learnCount = sailorsCourseLearnRecordMapper.selectSailorsCourseLearnCountBySaIdAndCourseIds(sailors.getUserId(), courseIdList);
            //必学课程没有学完
            if (learnCount < courseIdList.size()) {
                pendingsTotal += 1;
                pendingSailors.add(sailors);
                continue;
            }
            int totalCoursesCompletion = getTotalCoursesCompletion(sailors, pgciCoursesList);
            if (totalCoursesCompletion < pgciCoursesList.size()) {
                pendingsTotal += 1;
                pendingSailors.add(sailors);
            }
        }
        return pendingsTotal;
    }

    /**
     *  获取船员强制完成的课程数
     * @param sailors 船员
     * @param pgciCoursesList 需要强制完成的课程
     * @return
     */
    private int getTotalCoursesCompletion(PgciSailors sailors, List<PgciCourses> pgciCoursesList) {
        //完成的课程数
        int totalCoursesCompletion = 0;
        for (PgciCourses pgciCourses : pgciCoursesList) {
            //查询课程学习记录
            PgciSailorsCourseLearnRecord sailorsCourseLearnRecord = new PgciSailorsCourseLearnRecord();
            sailorsCourseLearnRecord.setUserId(sailors.getUserId());
            sailorsCourseLearnRecord.setCourseId(pgciCourses.getId());
            sailorsCourseLearnRecord.setCellValue("exclude");
            PgciSailorsCourseLearnRecord courseLearnRecord = sailorsCourseLearnRecordMapper.selectLastCourseLearnRecordListByUserIdAndCourseId(sailorsCourseLearnRecord);
            //只要有一个课程未学习就算未完成
            if (courseLearnRecord == null) {
                break;
            }
            int learnCycle = getLearnCycle(pgciCourses.getLearnRequire());
            if (learnCycle == 0) {
                continue;
            }
            String cellValue = courseLearnRecord.getCellValue();
            LocalDate learnDate = convertStrToLocalDate(cellValue);
            LocalDate today = LocalDate.now();
            LocalDate beforeDay = today.minusMonths(learnCycle);
            // beforeDay <= learnDate <= today 才算是完成课程
            long beforeBetween = ChronoUnit.DAYS.between(beforeDay, learnDate);
            long todayBetween = ChronoUnit.DAYS.between(learnDate, today);
            if (beforeBetween < 0 || todayBetween < 0) {
                break;
            }
            totalCoursesCompletion += 1;
        }
        return totalCoursesCompletion;
    }

    /**
     *  获取船员是否完成过非必学课程
     * @param sailors 船员
     * @param pgciCoursesList 非强制课程
     * @return
     */
    private boolean getIsCompletionNrCourse(PgciSailors sailors, List<PgciCourses> pgciCoursesList) {
        //完成的课程数
        boolean isCompletionNrCourse = false;
        for (PgciCourses pgciCourses : pgciCoursesList) {
            //查询课程学习记录
            PgciSailorsCourseLearnRecord sailorsCourseLearnRecord = new PgciSailorsCourseLearnRecord();
            sailorsCourseLearnRecord.setUserId(sailors.getUserId());
            sailorsCourseLearnRecord.setCourseId(pgciCourses.getId());
            sailorsCourseLearnRecord.setCellValue("exclude");
            PgciSailorsCourseLearnRecord courseLearnRecord = sailorsCourseLearnRecordMapper.selectLastCourseLearnRecordListByUserIdAndCourseId(sailorsCourseLearnRecord);
            //只要有一个课程未学习就算未完成
            if (courseLearnRecord == null) {
                continue;
            }
            int learnCycle = getLearnCycle(pgciCourses.getLearnRequire());
            if (learnCycle == 0) {
                continue;
            }
            String cellValue = courseLearnRecord.getCellValue();
            LocalDate learnDate = convertStrToLocalDate(cellValue);
            LocalDate today = LocalDate.now();
            LocalDate beforeDay = today.minusMonths(learnCycle);
            // beforeDay <= learnDate <= today 才算是完成课程
            long beforeBetween = ChronoUnit.DAYS.between(beforeDay, learnDate);
            long todayBetween = ChronoUnit.DAYS.between(learnDate, today);
            if (beforeBetween < 0 || todayBetween < 0) {
                continue;
            }
            isCompletionNrCourse = true;
             break;
        }
        return isCompletionNrCourse;
    }

    /**
     * 获取船员职位
     *
     * @param saRank    员职位
     * @param pgciPosts 职务表集合
     * @return
     */
    private PgciPost getsailorPost(String saRank, List<PgciPost> pgciPosts) {
        if (StringUtils.isBlank(saRank) || CollectionUtils.isEmpty(pgciPosts)) {
            return null;
        }
        //查询职位
        PgciPost post = pgciPosts.stream().filter(s -> saRank.equals(s.getPostName())).findFirst().orElse(null);
        if (post == null) {
            return null;
        }
        if (post.getParentId() == 0L) {
            return post;
        }

        //如果当前船员职务不是父级职务，通过子级查询父级别
        Long parentId = post.getParentId();
        post = pgciPosts.stream().filter(s -> s.getId().equals(parentId)).findFirst().orElse(null);
        return post;
    }

    /**
     * 需要完成强制性培训课程的总人数
     *
     * @return
     */
    public long getTotalPeopleMandatoryComplete() {
        List<PgciPost> pgciPosts = selectMandatoryCompletionCoursePost();
        if (CollectionUtils.isEmpty(pgciPosts)) {
            return 0;
        }
        List<String> postNameList = pgciPosts.stream().map(PgciPost::getPostName).collect(Collectors.toList());
        long count = pgciSailorsMapper.selectCountSailorsByRanks(postNameList);
        return count;
    }


    /**
     * 查询需要强制完成课程的岗位
     *
     * @return
     */
    public List<PgciPost> selectMandatoryCompletionCoursePost() {
        //parent post
        List<PgciPost> pgciPostParentList = pgciPostMapper.selectMandatoryCompletionCoursePost();
        if (CollectionUtils.isEmpty(pgciPostParentList)) {
            return pgciPostParentList;
        }
        //child post
        List<Long> postIds = pgciPostParentList.stream().map(PgciPost::getId).collect(Collectors.toList());
        List<PgciPost> childPosts = pgciPostMapper.selectChildPost(postIds);
        if (CollectionUtils.isNotEmpty(childPosts)) {
            pgciPostParentList.addAll(childPosts);
        }
        return pgciPostParentList;
    }

    private LocalDate convertStrToLocalDate(String str) {
        try {
            // 定义日期格式化模式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");

            // 将字符串转换为 LocalDate 类型
            LocalDate date = LocalDate.parse(str, formatter);

            return date;
        } catch (Exception e) {
            log.error("日期转化失败,{}", e.getMessage());
            return null;
        }
    }

    /**
     * 学习要求
     * 0. NO relation
     * 1.To Be Completed Within One Month of Joining #7a7a7a
     * 2.Mandatory Completed Every 12 Months  #660033
     * 3.Mandatory Completed Every 24 Months #ff0000
     * 4.Recommended #3366ff
     * 5.Combined = Safety Officer Course #92d050
     */
    private int getLearnCycle(Integer learnRequire) {
        if (learnRequire == null) {
            return 0;
        }
        if (learnRequire == 1) {
            return 1;
        } else if (learnRequire == 2) {
            return 12;
        } else if (learnRequire == 3) {
            return 24;
        }
        return 0;
    }

    private String getEnMoath(int month){
        String monthEn = "Jan";
        switch (month) {
            case 1:
                monthEn = "Jan";
                break;
            case 2:
                monthEn = "Feb";
                break;
            case 3:
                monthEn = "Mar";
                break;
            case 4:
                monthEn = "Apr";
                break;
            case 5:
                monthEn = "May";
                break;
            case 6:
                monthEn = "Jun";
                break;
            case 7:
                monthEn = "Jul";
                break;
            case 8:
                monthEn = "Aug";
                break;
            case 9:
                monthEn = "Sep";
                break;
            case 10:
                monthEn = "Oct";
                break;
            case 11:
                monthEn = "Nov";
                break;
            case 12:
                monthEn = "Dec";
                break;
        }
        return monthEn;
    }
}
